package com.huaxin.device.mapping;

import com.huaxin.device.dto.request.MetricCodeDictCreateReqDTO;
import com.huaxin.device.dto.request.MetricCodeDictUpdateReqDTO;
import com.huaxin.device.models.MetricCodeDict;
import com.huaxin.hxenum.enums.DataType;
import com.huaxin.hxenum.enums.Unit;
import org.mapstruct.AfterMapping;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;

@Mapper(componentModel = "spring")
public interface MetricCodeDictMapping {

    @Mapping(target = "dataType", ignore = true)
    @Mapping(target = "unit", ignore = true)
    MetricCodeDict toEntity(MetricCodeDictCreateReqDTO in);

    @AfterMapping
    default void afterMapping(MetricCodeDictCreateReqDTO in, @MappingTarget MetricCodeDict entity) {
        if (in.getDataType() != null) {
            entity.setDataType(DataType.of(in.getDataType()));
        }
        if (in.getUnit() != null) {
            entity.setUnit(Unit.of(in.getUnit()));
        }
    }

    @Mapping(target = "dataType", ignore = true)
    @Mapping(target = "unit", ignore = true)
    void updateEntity(MetricCodeDictUpdateReqDTO in, @MappingTarget MetricCodeDict entity);

    @AfterMapping
    default void afterMapping(MetricCodeDictUpdateReqDTO in, @MappingTarget MetricCodeDict entity) {
        if (in.getDataType() != null) {
            entity.setDataType(DataType.of(in.getDataType()));
        }
        if (in.getUnit() != null) {
            entity.setUnit(Unit.of(in.getUnit()));
        }
    }
}